package com.elbis.business.controller;

import java.io.Serializable;

import javax.faces.context.FacesContext;

import org.apache.log4j.Logger;

import com.elbis.action.input.ErrorMessages;
import com.elbis.action.input.ErrorsHashMap;
import com.elbis.action.output.Pages;
import com.elbis.business.controller.msg.FormFields;
import com.elbis.business.entity.Gamer;
import com.elbis.business.manager.ManageGamer;
import com.elbis.business.manager.MessageManager;
import com.elbis.business.model.ReturnTypes;
import com.elbis.business.util.ErrorMessageUtil;

public class LoginController implements Serializable {

	private static final long serialVersionUID = -8357311860838151171L;

	private static final Logger logger = Logger
			.getLogger(LoginController.class);

	private LanguageController languageBean = null;

	private String email;

	private String password = null;

	private Gamer gamer = null;

	public LanguageController getLanguageBean() {
		return languageBean;
	}

	public void setLanguageBean(LanguageController languageBean) {
		this.languageBean = languageBean;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String login() {

		if (email == null || email.isEmpty() || password == null
				|| password.isEmpty()) {
			logger.info(ErrorsHashMap
					.getMessage(ErrorMessages.WRONG_EMAIL_OR_PASSWORD));
			ErrorMessageUtil.addErrorMessage(
					FormFields.LOGIN,
					MessageManager.getErrorMessage(
							ErrorMessages.WRONG_EMAIL_OR_PASSWORD,
							languageBean.getLocaleCode()).getMessage());
			return null;
		}

		ManageGamer mg = new ManageGamer();
		gamer = mg.getUser(email, password);
		if (gamer == null) {
			logger.info(ErrorsHashMap
					.getMessage(ErrorMessages.WRONG_EMAIL_OR_PASSWORD));
			ErrorMessageUtil.addErrorMessage(
					FormFields.LOGIN,
					MessageManager.getErrorMessage(
							ErrorMessages.WRONG_EMAIL_OR_PASSWORD,
							languageBean.getLocaleCode()).getMessage());
			return null;
		} else {
			if (!gamer.isActive()) {
				logger.info(ErrorsHashMap
						.getMessage(ErrorMessages.ACCOUNT_NOT_ACTIVATED));
				ErrorMessageUtil.addErrorMessage(
						FormFields.LOGIN,
						MessageManager.getErrorMessage(
								ErrorMessages.ACCOUNT_NOT_ACTIVATED,
								languageBean.getLocaleCode()).getMessage());
				gamer = null;
				return null;
			} else {
				FacesContext.getCurrentInstance().getExternalContext()
						.getSessionMap().put(ReturnTypes.USER, gamer);
				setUserLocale(gamer);
			}
		}
		return Pages.IDENTIFIED;
	}

	private void setUserLocale(Gamer gamer) {
		if (gamer.getLanguage() != null) {
			languageBean.setLocale(gamer.getLanguage());
		}
	}

	public String logout() {
		this.gamer = null;
		FacesContext context = FacesContext.getCurrentInstance();
		context.getExternalContext().invalidateSession();
		return Pages.WELCOME;
	}

	public boolean isLoggedIn() {
		return gamer != null;
	}

	public Gamer getCurrentGamer() {
		return gamer;
	}

	public String viewGamer() {
		return Pages.VIEW_USER;
	}
}
